package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.BrandDTO;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public List<BrandDTO> findBradsByBid(List<Long> id) {
        List<Brand> brands = baseMapper.selectBatchIds(id);
        return BrandDTO.convertEntityList(brands);
    }

    //品牌id查询分类id
    @Override
    public List<BrandDTO> findBrandByCid(Long id) {
        List<Brand> brandByCid = baseMapper.findBrandByCid(id);
        return BrandDTO.convertEntityList(brandByCid);
    }

    //根据id删除品牌
    @Override
    public void deleteBrandById(Long id) {
       /* QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("brand_id",id);
        baseMapper.delete(queryWrapper);*/
        baseMapper.deleteBrandById(id);
        //删除tb_brand的数据
        //baseMapper.selectById(id);
    }

    // page 当前页码,row 每页大小,key 是搜索条件
    @Override
    public PageDTO<BrandDTO> findPagesByHelper(String key, Integer page, Integer rows) {

        //分页条件
        Page<Brand> pageInfo = new Page<>(page,rows);
        boolean exists = StringUtils.isNotBlank(key);
        query()
        .like(exists,"name",key)
        .or()
        .eq(exists,"letter",key)
        .page(pageInfo);
        List<Brand> records = pageInfo.getRecords();
        return new PageDTO<>(pageInfo.getTotal(),pageInfo.getPages(),BrandDTO.convertEntityList(records));
    }
    //增加品牌
    @Override
    public void addBrand(BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        //存到brand表中

        if(this.save(brand)){
        //存到中间表中
        List<CategoryBrand> collect = brandDTO.getCategoryIds().stream().map(id ->CategoryBrand.of(id, brand.getId())).collect(Collectors.toList());
        categoryBrandService.saveBatch(collect);
        }
    }

    //更新品牌
    @Override
    @Transactional
    public void updateBrand(BrandDTO brandDTO, List<Long> categoryIds) {

        Brand brand = brandDTO.toEntity(Brand.class);
        //修改brand
        updateById(brand);

        if(this.updateById(brand)) {
            //删除中间表
//            QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("brand_id",brand.getId());
            boolean brand_id = categoryBrandService
           .remove(new QueryWrapper<CategoryBrand>()
           .eq("brand_id",brand.getId()));
        }
        //更新中间表
        List<CategoryBrand> collect = brandDTO.getCategoryIds().stream().map(id -> CategoryBrand.of(id, brand.getId())).collect(Collectors.toList());
        categoryBrandService.saveBatch(collect);
    }
    //更新品牌
  /*  @Override
    public void updateBrand(BrandDTO brandDTO) {
        //更新品牌
//        Brand brand = brandDTO.toEntity(Brand.class);
//        brand.setName(brandDTO.getName());
//        brand.setImage(brandDTO.getImage());
//        brand.setLetter(brandDTO.getLetter());
//        save(brand);
          //更新表要先删除中间表的数据
        Brand brand = brandDTO.toEntity(Brand.class);
        QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper<>();
        boolean brand_id = categoryBrandService.remove(queryWrapper.eq("brand_id", brand.getId()));
        //
    }*/

}
